package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.CrossActivityRankProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_GET_ACTIVITY_RANK, desc = "查看跨服活动榜单")
public class GetCrossActivityRankListCmd extends CrossCommand {
    private long userId;
    private int activityId;
    private int bigRankType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityRankProto.CrossActivityRankReqMsg reqMsg = CrossActivityRankProto.CrossActivityRankReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        bigRankType = reqMsg.getBigRankType();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        RankProto.RankListRespMsg.Builder respMsg = RankProto.RankListRespMsg.newBuilder();
        respMsg.setRankType(activityId);
        respMsg.setMyRank(-1);
        respMsg.setBigRankType(bigRankType);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityRankListModel(activityId);
        RankProto.RankListMsg.Builder rankListMsg = RankProto.RankListMsg.newBuilder();
        if (rankListModel != null) {
            rankListMsg = rankListModel.getRankListMsg();
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
            if (null != activityInfo) {
                int conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
                maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
            }
            while (null != rankListMsg.getRankListList() && rankListMsg.getRankListCount() > maxRank) {
                rankListMsg.removeRankList(rankListMsg.getRankListCount() - 1);
            }
            if (rankListMsg.getRankListList() != null) {
                respMsg.setMyRank(getMyRank(userId, serverId, rankListMsg.getRankListList(), maxRank));
            }
        }
        RankProto.RankListMsg.Builder tempMsg = RankProto.RankListMsg.newBuilder();
        int num = 0;
        for (RankProto.RankTempMsg msg : rankListMsg.getRankListList()) {
            tempMsg.addRankList(msg);
            num++;
            if (num >= ActivityMgr.getRankListShowNum()) {
                break;
            }
        }
        respMsg.setRankList(tempMsg);
        YanQuMessage msg = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_ACTIVITY_RANK_LIST, respMsg);
        MessageHelper.sendPacket(serverId, userId, msg);
    }

    public int getMyRank(long userId, long serverId, List<RankProto.RankTempMsg> rankListList, int maxRank) {
        int rank = -1;
        int index = 0;
        synchronized (this) {
            for(RankProto.RankTempMsg userRank : rankListList){
                if(userRank.getUserId() == userId && userRank.getPlayerBaseData().getServerId() == serverId){
                    rank = index + 1;
                    break;
                }
                index++;
            }
        }
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    @Override
    public void sendData() {

    }
}
